package cn.sfrog.project.biz.admin.utils;

import java.util.regex.Pattern;


public class CheckBankCardUtils2 {

    private final static Pattern bank_reg_p = Pattern.compile("^\\d{12,26}$");
    private final static Pattern bank_reg_p_find = Pattern.compile("\\d{12,}");

    /**
     * 判断是否为银行卡号
     *
     * @param dataString
     * @return
     */
    public static boolean support(String dataString) {
        int length = dataString.length();
        return isBankCard(dataString) && getBankCardCheckCode(dataString.substring(0, length - 1), dataString.charAt(length - 1));

    }

    public static boolean isBankCard(String dataString) {
        return bank_reg_p.matcher(dataString).matches();
    }

    public static Pattern getRegFind() {
        return bank_reg_p_find;
    }

    /**
     * 银行卡校验位计算，并验证
     *
     * @param nonCheckCodeCardId
     * @param c
     * @return
     */
    private static boolean getBankCardCheckCode(String nonCheckCodeCardId, char c) {

        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }

        return ((luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0')) == c;
    }

    public static void main(String[] args) {
        boolean support = support("6222005865412565805");
        System.out.println(support);
    }
}
